/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VLCPlayerComponent;

import VLCPlayerComponent.VLCFrame;
import Dispays.DisplayManager;
import PlayerComponent.JPlayerReport;
import PlayerShell.JMplayerLoader;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import uk.co.caprica.vlcj.binding.internal.libvlc_media_t;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventListener;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

/**
 *
 * @author wcss
 */
public final class VLCMediaPlayer extends javax.swing.JPanel implements MediaPlayerEventListener {

    public JPlayerReport playerEvents = null;
    public EmbeddedMediaPlayer currentPlayers;
    private Boolean paused = false;
    public long currentPosition = 0;
    private Boolean fullscreened = false;
    public int currentVolume = 100;
    protected VLCPlayerInternalFrame currentInternalFrame = null;
    public String currentMediaUrl = "";

    public void setWelcomeText(String weltext)
    {
        this.lblWelcome.setText(weltext);
    }
    
    /**
     * Creates new form VLCMediaPlayer
     */
    public VLCMediaPlayer() {
        initComponents();
        //showWelcome();
        //initPlayer();
    }

    public void showWelcome() {
        this.dpPlayerContainer.setVisible(false);
        this.lblWelcome.setLocation(0, 0);
        this.lblWelcome.setSize(this.getWidth(), this.getHeight());
        this.lblWelcome.setVisible(true);
        this.lblWelcome.setLocation(0, 0);
        this.lblWelcome.setSize(this.getWidth(), this.getHeight());
    }

    public void showPlayer() {
        this.lblWelcome.setVisible(false);
        this.dpPlayerContainer.setLocation(0, 0);
        this.dpPlayerContainer.setSize(this.getWidth(), this.getHeight());
        this.dpPlayerContainer.setVisible(true);
        this.dpPlayerContainer.setLocation(0, 0);
        this.dpPlayerContainer.setSize(this.getWidth(), this.getHeight());
    }

    public void initPlayer() {
        currentInternalFrame = new VLCPlayerInternalFrame();
        this.dpPlayerContainer.add(currentInternalFrame);
        currentInternalFrame.setLocation(0, 0);
        currentInternalFrame.setSize(this.dpPlayerContainer.getWidth(), this.dpPlayerContainer.getHeight());
        currentInternalFrame.setMaximizable(true);
        currentInternalFrame.setContentPane(((VLCFrame) playerEvents).mediaPlayerComponent);
        currentInternalFrame.setVisible(true);
    }
    
    public void initPlayerWithTitle() {
        currentInternalFrame = new VLCPlayerInternalFrame();
        this.dpPlayerContainer.add(currentInternalFrame);
        currentInternalFrame.setLocation(0, 0);
        currentInternalFrame.setSize(this.dpPlayerContainer.getWidth(), this.dpPlayerContainer.getHeight());
        currentInternalFrame.setMaximizable(true);
        currentInternalFrame.setContentPane(((VLCTitleFrame) playerEvents).mediaPlayerComponent);
        currentInternalFrame.setVisible(true);
    }

    public void playMovie(String moviepath) {
        stopPlay();
        showPlayer();
        paused = false;
        fullscreened = false;
        currentMediaUrl = moviepath;
        currentPlayers = ((VLCFrame) playerEvents).mediaPlayerComponent.getMediaPlayer();
        currentPlayers.playMedia(moviepath);
        currentPlayers.addMediaPlayerEventListener(this);
        currentVolume = 100;
        currentPlayers.setVolume(currentVolume);
    }
    
    public void playMovieWithTitle(String moviepath) {
        stopPlay();
        showPlayer();
        paused = false;
        fullscreened = false;
        currentMediaUrl = moviepath;
        currentPlayers = ((VLCTitleFrame) playerEvents).mediaPlayerComponent.getMediaPlayer();
        currentPlayers.playMedia(moviepath);
        currentPlayers.addMediaPlayerEventListener(this);
        currentVolume = 100;
        currentPlayers.setVolume(currentVolume);
    }

    public void stopPlay() {
        if (currentPlayers != null) {
            currentPlayers.stop();
            //currentPlayers.release();
            currentPlayers = null;
            showWelcome();
        }
    }

    public void pausePlay() {
        if (currentPlayers != null) {
            if (paused) {
                currentPlayers.play();
            } else {
                currentPlayers.pause();
            }
            paused = !paused;
        }
    }

    public void mutePlay() {

        if (currentPlayers != null) {
            currentPlayers.mute();
        }
    }

    public void seekPlay(int playPosPercent) {

        if (currentPlayers != null) {
            currentPosition = playPosPercent;
            currentPlayers.setPosition((float) currentPosition / 100);
        }
    }

    public void getPlayPercent() {
        if (currentPlayers != null) {
            this.reportPlayProgress((int) (currentPlayers.getPosition() * 100));
        }
    }

    public void getTimeLength() {
        if (currentPlayers != null) {
            //this.reportPlayProgress((int)(currentPlayers.getPosition() * 100));
        }
    }

    public void getTimePos() {
        if (currentPlayers != null) {
            //this.reportPlayProgress((int)(currentPlayers.getPosition() * 100));
        }
    }

    public void fullScreenPlay() {
        if (currentPlayers != null) {

            fullscreened = !fullscreened;
            this.stopPlay();
            VLCFullScreenFrame fullscreen = new VLCFullScreenFrame();
            fullscreen.getMediaPlayer().playMovie(currentMediaUrl);
            fullscreen.getMediaPlayer().seekPlay((int)this.currentPosition);
        }
    }

    public void moveVolumeToUp() {
//        sendPlayerCmd("volume 10 \n");
//        this.reportPlayAction("volume+");
        if (currentPlayers != null) {
            if (currentVolume + 20 <= 200) {
                currentVolume += 20;
                currentPlayers.setVolume(currentVolume);
            }
        }
    }

    public void moveVolumeToDown() {
        if (currentPlayers != null) {
            if (currentVolume - 20 >= 0) {
                currentVolume -= 20;
                currentPlayers.setVolume(currentVolume);
            }
        }
    }

    public void jumpVolumeToUp(int step) {
        if (currentPlayers != null) {
            int volume = 0;
            for (int k = 0; k < step; k++) {
                currentVolume += 20;
            }

            currentPlayers.setVolume(currentVolume);
            this.reportPlayAction("volume ++");
        }
    }

    public void jumpVolumeToDown(int step) {
        if (currentPlayers != null) {
            int volume = 0;
            for (int k = 0; k < step; k++) {
                currentVolume -= 20;
            }

            currentPlayers.setVolume(currentVolume);
            this.reportPlayAction("volume ++");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblWelcome = new javax.swing.JLabel();
        dpPlayerContainer = new javax.swing.JDesktopPane();

        setBackground(java.awt.Color.black);
        setLayout(null);

        lblWelcome.setBackground(java.awt.Color.black);
        lblWelcome.setFont(new java.awt.Font("DejaVu Sans YuanTi", 0, 48)); // NOI18N
        lblWelcome.setForeground(java.awt.Color.white);
        lblWelcome.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblWelcome.setText("Media Player");
        add(lblWelcome);
        lblWelcome.setBounds(0, 0, 880, 490);

        dpPlayerContainer.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                dpPlayerContainerComponentResized(evt);
            }
        });
        add(dpPlayerContainer);
        dpPlayerContainer.setBounds(849, 459, 0, 0);
    }// </editor-fold>//GEN-END:initComponents

    private void dpPlayerContainerComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_dpPlayerContainerComponentResized
        // TODO add your handling code here:
        if (this.currentInternalFrame != null) {
            currentInternalFrame.setLocation(0, 0);
            currentInternalFrame.setSize(this.dpPlayerContainer.getWidth(), this.dpPlayerContainer.getHeight());
        }
    }//GEN-LAST:event_dpPlayerContainerComponentResized
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane dpPlayerContainer;
    private javax.swing.JLabel lblWelcome;
    // End of variables declaration//GEN-END:variables

    protected void reportPlayProgress(int percent) {
        if (playerEvents != null) {
            this.playerEvents.reportPlayProgress(this, percent);
        }
    }

    protected void reportPlayAction(String action) {
        if (playerEvents != null) {
            playerEvents.reportPlayAction(this, action);
        }
    }

    @Override
    public void mediaChanged(MediaPlayer mp, libvlc_media_t l, String string) {
        System.out.println("准备播放电影，路径：" + string);
    }

    @Override
    public void opening(MediaPlayer mp) {
        System.out.println("正在打开......");
    }

    @Override
    public void buffering(MediaPlayer mp, float f) {
        System.out.println("正在缓冲......");
    }

    @Override
    public void playing(MediaPlayer mp) {
        System.out.println("正在播放......");
    }

    @Override
    public void paused(MediaPlayer mp) {
        System.out.println("已经暂停......");
    }

    @Override
    public void stopped(MediaPlayer mp) {
        System.out.println("已经停止......");
    }

    @Override
    public void forward(MediaPlayer mp) {
        System.out.println("前进......");
    }

    @Override
    public void backward(MediaPlayer mp) {
        System.out.println("后退......");
    }

    @Override
    public void finished(MediaPlayer mp) {
        System.out.println("播放完成......");
    }

    @Override
    public void timeChanged(MediaPlayer mp, long l) {
        //System.out.println("时间改变-" + l + "......");
    }

    @Override
    public void positionChanged(MediaPlayer mp, float f) {
        //System.out.println("进度改变......");
        getPlayPercent();
    }

    @Override
    public void seekableChanged(MediaPlayer mp, int i) {
        //System.out.println("已经暂停......");
    }

    @Override
    public void pausableChanged(MediaPlayer mp, int i) {
        // System.out.println("已经暂停......");
    }

    @Override
    public void titleChanged(MediaPlayer mp, int i) {
        System.out.println("标题改变......");
    }

    @Override
    public void snapshotTaken(MediaPlayer mp, String string) {
        System.out.println("已经暂停......");
    }

    @Override
    public void lengthChanged(MediaPlayer mp, long l) {
        System.out.println("长度改变......");
    }

    @Override
    public void videoOutput(MediaPlayer mp, int i) {
        System.out.println("视频输出......");
    }

    @Override
    public void error(MediaPlayer mp) {
        System.out.println("出错......");
    }

    @Override
    public void mediaMetaChanged(MediaPlayer mp, int i) {
        System.out.println("已经暂停......");
    }

    @Override
    public void mediaSubItemAdded(MediaPlayer mp, libvlc_media_t l) {
        System.out.println("已经暂停......");
    }

    @Override
    public void mediaDurationChanged(MediaPlayer mp, long l) {
        System.out.println("已经暂停......");
    }

    @Override
    public void mediaParsedChanged(MediaPlayer mp, int i) {
        System.out.println("已经暂停......");
    }

    @Override
    public void mediaFreed(MediaPlayer mp) {
        System.out.println("已经暂停......");
    }

    @Override
    public void mediaStateChanged(MediaPlayer mp, int i) {
        System.out.println("已经暂停......");
    }

    @Override
    public void newMedia(MediaPlayer mp) {
        System.out.println("新的媒体......");
    }

    @Override
    public void subItemPlayed(MediaPlayer mp, int i) {
        System.out.println("已经暂停......");
    }

    @Override
    public void subItemFinished(MediaPlayer mp, int i) {
        System.out.println("已经暂停......");
    }

    @Override
    public void endOfSubItems(MediaPlayer mp) {
        System.out.println("已经暂停......");
    }
}
